package com.banshuibao.bsb_android.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2018/5/23.
 */

public class TaxFirstAdapter extends RecyclerView.Adapter<TaxFirstAdapter.ViewHolder> {

    private Activity context;

    private List<String> list;

    private String type;

    public TaxFirstAdapter(Activity context, String type) {
        this.context = context;
        list = new ArrayList<>();
        this.type = type;
    }

    public void addData(List<String> list) {
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_tax_type, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        String money = list.get(position);
        if (type.equals(money))
            holder.textView.setTextColor(context.getResources().getColor(R.color.counter_blue));
        else
            holder.textView.setTextColor(context.getResources().getColor(R.color.text_black));
        holder.textView.setText(money);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent();
                    it.putExtra("money", textView.getText().toString());
                    context.setResult(Activity.RESULT_OK, it);
                    context.finish();
                }
            });
        }
    }

    public void clear() {
        if (this.list != null) {
            list.clear();
        }
    }
}
